<template>
  <div class="side-menu-container">
    <!-- 个人资料 -->
    <div class="profile-section">
      <div class="profile-header">
        <div class="avatar-placeholder"></div>
        <div class="profile-info">
          <div class="username">张三</div>
          <div class="status">
            <span class="status-dot online"></span>
            <span class="status-text">在线</span>
          </div>
        </div>
      </div>
      <div class="signature">今天的阳光真好</div>
    </div>

    <!-- 导航菜单 -->
    <div class="menu-section">
      <el-menu
        :default-active="activeIndex"
        class="el-menu-vertical"
        @select="handleSelect"
      >
        <!-- 消息 -->
        <el-sub-menu index="messages">
          <template #title>
            <i class="el-icon-message"></i>
            <span>消息</span>
          </template>
          <el-menu-item index="messages-1">张三 <span class="unread-count">2</span></el-menu-item>
          <el-menu-item index="messages-2">李四 <span class="unread-count">1</span></el-menu-item>
          <el-menu-item index="messages-3">王五</el-menu-item>
        </el-sub-menu>

        <!-- 好友 -->
        <el-sub-menu index="friends">
          <template #title>
            <i class="el-icon-user"></i>
            <span>好友</span>
          </template>
          <el-menu-item index="friends-all">全部好友 (15)</el-menu-item>
          <el-menu-item index="friends-requests">好友请求 (3)</el-menu-item>
        </el-sub-menu>

        <!-- 群组 -->
        <el-sub-menu index="groups">
          <template #title>
            <i class="el-icon-user-group"></i>
            <span>群组</span>
          </template>
          <el-menu-item index="groups-1">技术交流群 <span class="unread-count">5</span></el-menu-item>
          <el-menu-item index="groups-2">同学群 <span class="unread-count">2</span></el-menu-item>
          <el-menu-item index="groups-3">家人群 <span class="unread-count">0</span></el-menu-item>
        </el-sub-menu>

        <!-- 聊天室 -->
        <el-sub-menu index="rooms">
          <template #title>
            <i class="el-icon-chat-dot-square"></i>
            <span>聊天室</span>
          </template>
          <el-menu-item index="rooms-1">技术讨论室</el-menu-item>
          <el-menu-item index="rooms-2">音乐分享室</el-menu-item>
          <el-menu-item index="rooms-3">游戏大厅</el-menu-item>
        </el-sub-menu>

        <!-- 设置 -->
        <el-menu-item index="settings">
          <i class="el-icon-setting"></i>
          <span>设置</span>
        </el-menu-item>
      </el-menu>
    </div>

    <!-- 在线人数 -->
    <div class="online-users">
      在线: 15
    </div>
  </div>
</template>

<script>
export default {
  name: "SideMenu",
  data() {
    return {
      activeIndex: 'messages-1'
    };
  },
  watch: {
    '$route.path': {
      immediate: true,
      handler(newPath) {
        // 根据路由更新激活的菜单项
        switch (newPath) {
          case '/messages':
            this.activeIndex = 'messages-1';
            break;
          case '/friends':
            this.activeIndex = 'friends-all';
            break;
          case '/groups':
            this.activeIndex = 'groups-1';
            break;
          case '/rooms':
            this.activeIndex = 'rooms-1';
            break;
          case '/settings':
            this.activeIndex = 'settings';
            break;
          default:
            this.activeIndex = 'messages-1';
        }
      }
    }
  },
  methods: {
    handleSelect(index) {
      // 根据菜单项索引导航到相应页面
      switch (index) {
        case 'messages-1':
        case 'messages-2':
        case 'messages-3':
          this.$router.push('/messages');
          break;
        case 'friends-all':
          this.$router.push('/friends');
          break;
        case 'friends-requests':
          this.$router.push('/friends/requests');
          break;
        case 'groups-1':
        case 'groups-2':
        case 'groups-3':
          this.$router.push('/groups');
          break;
        case 'rooms-1':
        case 'rooms-2':
        case 'rooms-3':
          this.$router.push('/rooms');
          break;
        case 'settings':
          this.$router.push('/settings');
          break;
      }
      
      // 在移动端点击菜单项后隐藏菜单
      if (window.innerWidth < 768) {
        const sideMenu = document.querySelector('.side-menu');
        if (sideMenu) {
          sideMenu.classList.remove('mobile-visible');
        }
      }
    }
  }
};
</script>

<style scoped>
.side-menu-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: var(--white);
}

.profile-section {
  padding: var(--spacing-md) var(--spacing-sm);
  border-bottom: 1px solid var(--border-color);
}

.profile-header {
  display: flex;
  align-items: center;
  margin-bottom: var(--spacing-sm);
}

.avatar-placeholder {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: var(--spacing-sm);
  background-color: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 24px;
}

.profile-info {
  flex: 1;
}

.username {
  font-size: 16px;
  font-weight: var(--font-weight-bold);
  color: var(--text-primary);
  margin-bottom: var(--spacing-xs);
}

.status {
  display: flex;
  align-items: center;
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: var(--spacing-xs);
}

.status-dot.online {
  background-color: #52c41a;
}

.status-text {
  font-size: 12px;
  color: #666;
}

.signature {
  font-size: 12px;
  color: #999;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.menu-section {
  flex: 1;
  overflow-y: auto;
}

.el-menu-vertical {
  border-right: none;
}

.el-menu-item, .el-sub-menu__title {
  height: calc(2 * var(--spacing-lg) + var(--spacing-md));
  line-height: calc(2 * var(--spacing-lg) + var(--spacing-md));
  transition: all 0.3s ease;
}

.el-menu-item:hover, .el-sub-menu__title:hover {
  background-color: #f0f7ff;
  /* 添加悬停效果类 */
  /* menu-item-hover-effect */
}

.el-menu-item i, .el-sub-menu__title i {
  margin-right: var(--spacing-sm);
  color: #666;
}

.unread-count {
  display: inline-block;
  padding: 2px 6px;
  background-color: #ff4d4f;
  color: white;
  font-size: 12px;
  border-radius: 10px;
  margin-left: var(--spacing-xs);
}

.online-users {
  padding: var(--spacing-sm);
  text-align: center;
  font-size: 14px;
  color: var(--text-secondary);
  border-top: 1px solid var(--border-color);
}

/* 移动端优化 */
@media (max-width: 767px) {
  .profile-section {
    padding: var(--spacing-sm);
  }
  
  .profile-header {
    margin-bottom: var(--spacing-xs);
  }
  
  .avatar-placeholder {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
  
  .username {
    font-size: 14px;
    margin-bottom: var(--spacing-xs);
  }
  
  .signature {
    font-size: 11px;
  }
  
  .el-menu-item,
  .el-sub-menu__title {
    height: calc(var(--spacing-lg) + var(--spacing-md));
    line-height: calc(var(--spacing-lg) + var(--spacing-md));
    font-size: 14px;
  }
  
  .el-menu-item i,
  .el-sub-menu__title i {
    font-size: 16px;
  }
  
  .unread-count {
    font-size: 10px;
    padding: 1px 4px;
  }
  
  .online-users {
    padding: var(--spacing-xs);
    font-size: 12px;
  }
}
</style>